﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Core.Services
{
	using System;
	using System.Collections.Generic;
	using System.Globalization;
	using Falcon.Core.Collections;
	using Falcon.Core.Domain;
	using Falcon.Core.Domain.Repositories;
	using Falcon.Core.Domain.Services;
	using Falcon.Core.Domain.Values;
	using Falcon.Core.Extensions;
	using TimeZone = Falcon.Core.Domain.TimeZone;

	public class TimeZoneService : ITimeZoneService
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="TimeZoneService"/> class.
		/// </summary>
		/// <param name="timeZoneRepository">The time zone repository.</param>
		public TimeZoneService(ITimeZoneRepository timeZoneRepository)
		{
			this.TimeZoneRepository = timeZoneRepository;
		}

		/// <summary>
		/// Gets or sets the time zone repository.
		/// </summary>
		/// <value>The time zone repository.</value>
		public ITimeZoneRepository TimeZoneRepository
		{
			get; 
			private set;
		}

		/// <summary>
		/// Gets the time zone by the specified specification.
		/// </summary>
		/// <param name="specification">The <see cref="TimeZoneSpecification"/>.</param>
		/// <returns></returns>
		public TimeZone GetBy(TimeZoneSpecification specification)
		{
			return this.TimeZoneRepository.GetBy(specification);
		}

		/// <summary>
		/// Gets the time zone by id.
		/// </summary>
		/// <param name="id">The id.</param>
		/// <returns></returns>
		public TimeZone GetById(int id)
		{
			return this.TimeZoneRepository.GetById(id);
		}

		/// <summary>
		/// Gets all the time zones.
		/// </summary>
		/// <returns></returns>
		public IList<TimeZone> GetAll()
		{
			return this.TimeZoneRepository.GetAll();
		}

		/// <summary>
		/// Gets all the time zones by the specified specification.
		/// </summary>
		/// <param name="specification">The <see cref="CountrySpecification"/>.</param>
		/// <returns></returns>
		public IPagedList<TimeZone, TimeZoneSpecification> GetPaged(TimeZoneSpecification specification)
		{
			return this.TimeZoneRepository.GetPaged(specification);
		}

		/// <summary>
		/// Gets the count of time zones.
		/// </summary>
		/// <returns></returns>
		public int Count()
		{
			return this.TimeZoneRepository.Count();
		}

		/// <summary>
		/// Creates the time zone.
		/// </summary>
		/// <returns></returns>
		public TimeZone Create()
		{
			return new TimeZone();
		}

		/// <summary>
		/// Populates the storage for the first time.
		/// </summary>
		public void Setup()
		{
			var values = TimeZoneInfo.GetSystemTimeZones();

			foreach (var value in values)
			{
				var timeZone = this.Create();

				timeZone.Title = value.Id;
				timeZone.Display = value.DisplayName.Replace("GMT", "GMT ");

				var offset = string.Concat(
					value.BaseUtcOffset.Hours.ToString(CultureInfo.InvariantCulture),
					".",
					value.BaseUtcOffset.Minutes.ToString(CultureInfo.InvariantCulture).Replace("-", ""));

				timeZone.Offset = Convert.ToDouble(offset, CultureInfo.InvariantCulture);

				this.Insert(timeZone);
			}
		}

		/// <summary>
		/// Inserts the specified time zone.
		/// </summary>
		/// <param name="timeZone">The <see cref="TimeZone"/>.</param>
		public void Insert(TimeZone timeZone)
		{
			timeZone.Validate();

			timeZone.Slug = timeZone.Title.Slug();

			this.TimeZoneRepository.Insert(timeZone);
		}

		/// <summary>
		/// Inserts the specified time zone.
		/// </summary>
		/// <param name="timeZone">The time zone.</param>
		/// <param name="value">The value.</param>
		public void Insert(TimeZone timeZone, TimeZoneCreate value)
		{
			value.Validate();

			timeZone.Title = value.Title;
			timeZone.Display = value.Display;

			this.Insert(timeZone);
		}

		/// <summary>
		/// Updates the specified time zone.
		/// </summary>
		/// <param name="timeZone">The <see cref="TimeZone"/>.</param>
		public void Update(TimeZone timeZone)
		{
			timeZone.Validate();

			timeZone.Slug = timeZone.Title.Slug();

			this.TimeZoneRepository.Update(timeZone);
		}

		/// <summary>
		/// Updates the specified time zone.
		/// </summary>
		/// <param name="timeZone">The time zone.</param>
		/// <param name="value">The value.</param>
		public void Update(TimeZone timeZone, TimeZoneUpdate value)
		{
			value.Validate();

			timeZone.Title = value.Title;
			timeZone.Display = value.Display;

			this.Update(timeZone);
		}

		/// <summary>
		/// Deletes the specified time zone.
		/// </summary>
		/// <param name="timeZone">The <see cref="TimeZone"/>.</param>
		public void Delete(TimeZone timeZone)
		{
			this.TimeZoneRepository.Delete(timeZone);
		}
	}
}